package com.abu.edums.service.impl;

import com.abu.edums.mapper.CourseMapper;
import com.abu.pojo.Course;
import com.abu.pojo.Teacher;
import com.abu.edums.mapper.TeacherMapper;
import com.abu.edums.service.TeacherService;
import com.abu.utils.Resp;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2020-06-15
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Teacher> queryLimitByLevel() {

//        1.设置查询条件
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("level");
//        2.设置查询多少条
        Page<Teacher> teacherPage = new Page<>();
        teacherPage.setSize(4);
//        执行查询
        Page<Teacher> page = this.teacherMapper.selectPage(teacherPage, queryWrapper);
        return page.getRecords();
    }

    @Override
    public Map<String, Object> queryPage(Long page, Long limit) {
//       分页条件
        Page<Teacher> teacherPage = new Page<>(page, limit);

//        查询条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
//        执行查询
        Page<Teacher> pageParam = this.page(teacherPage, wrapper);
//        获取结果集
        List<Teacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
//      封装结果集
        HashMap<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
//      返回结果集
        return map;
    }

    @Override
    public Map<String, Object> queryTeacherAndCourses(String teacherId) {
        HashMap<String, Object> map = new HashMap<>();
//        查询讲师
        Teacher teacher = this.teacherMapper.selectById(teacherId);
//        查询讲师相关课程
        QueryWrapper<Course> wrapper = new QueryWrapper<Course>().eq("teacher_id", teacherId);
        String[] selects = {"id", "cover", "title"};
        wrapper.select(selects);
        List<Course> courseList = this.courseMapper.selectList(wrapper);
        map.put("teacher", teacher);
        map.put("courseList", courseList);
        return map;
    }
}
